/****************************************************************************** * Copyright (C) Ultraleap, Inc. 2011-2021. * * * * Use subject to the terms of the Apache License 2.0 available at * * http://www.apache.org/licenses/LICENSE-2.0, or another agreement * * between Ultraleap and you, your company or other organization. * ******************************************************************************/ using System; using UnityEngine; using UnityEngine.Events; using UnityEngine.Serialization; namespace Leap.Unity.Interaction { /// /// A physics-enabled toggle. Toggling is triggered by physically pushing the toggle to its compressed position. /// public class InteractionToggle : InteractionButton { [Tooltip("The height that this button rests at; this value is a lerp in between the min and max height.")] [Range(0f, 1f)] /// The height that this toggle rests at when it is toggled. public float toggledRestingHeight = 0.25f; [SerializeField, FormerlySerializedAs("_toggled")] private bool _isToggled = false; [SerializeField] private bool _startToggled = false; /// Whether or not this toggle is currently toggled. public bool isToggled { get { return _isToggled; } set { if (_isToggled != value) { _isToggled = value; if (_isToggled) { OnToggle(); } else { OnUntoggle(); } restingHeight = isToggled ? toggledRestingHeight : _originalRestingHeight; rigidbody.WakeUp(); _pressedThisFrame = value; _unpressedThisFrame = !value; } } } /// Triggered when this toggle is toggled. [FormerlySerializedAs("toggleEvent")] [SerializeField] private UnityEvent _toggleEvent = new UnityEvent(); /// /// Called when the toggle is ticked (not when unticked; for that, use OnUntoggle.) /// public Action OnToggle = () => { }; /// Triggered when this toggle is untoggled. [FormerlySerializedAs("unToggleEvent")] [SerializeField] public UnityEvent _untoggleEvent = new UnityEvent(); /// /// Called when the toggle is unticked. /// public Action OnUntoggle = () => { }; private float _originalRestingHeight; public float untoggledRestingHeight { get { return _originalRestingHeight; } } /// /// Returns the local position of this toggle when it is able to relax into its untoggled position. /// public Vector3 RelaxedToggledLocalPosition { get { return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, toggledRestingHeight); } } /// /// Returns the local position of this toggle when it is able to relax into its untoggled position. /// public override Vector3 RelaxedLocalPosition { get { if (!Application.isPlaying) { return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, restingHeight); } else { return initialLocalPosition + Vector3.back * Mathf.Lerp(minMaxHeight.x, minMaxHeight.y, untoggledRestingHeight); } } } protected override void Awake() { base.Awake(); _originalRestingHeight = restingHeight; } protected override void Start() { base.Start(); if (_startToggled) { isToggled = true; } OnToggle += _toggleEvent.Invoke; OnUntoggle += _untoggleEvent.Invoke; } protected override void OnEnable() { OnPress += OnPressed; base.OnEnable(); } protected override void OnDisable() { base.OnDisable(); OnPress -= OnPressed; } private void OnPressed() { isToggled = !isToggled; } /// /// Sets this InteractionToggle to the "toggled" state. Calling this function won't /// oscillate the state of the toggle; to 'untoggle' the control, call Untoggle(). /// public void Toggle() { isToggled = true; } /// /// Sets this InteractionToggle to the "untoggled" state. /// public void Untoggle() { isToggled = false; } } }